import json
import time
import socket
import datetime

from ..handle import Request
from ..utils import make_request
from ..pub_data import METHODS_INFO
from ..protocol import RequestHeaders, RequestBody, TIME_FMT


class Client:
    ENCODING = "utf-8"
    BUFFER_SIZE = 4096
    VERSION = 1

    def __init__(self, host=None, port=None):
        self.raddr = host, port
        self.sock = socket.socket()

        # default will be rewrite
        self.receive_headers = {
            "date": None, "version": self.VERSION, "accept": "any"
        }
        self.receive_body = {}

        if host and port:
            # if provide host and port, auto connect server
            self.connect()

    def connect(self, host=None, port=None):
        if host or port:
            self.raddr = host, port
        self.sock.connect(self.raddr)

    def _send(self, headers: dict, body: dict):
        now = datetime.datetime.now().strftime(TIME_FMT)

        self.receive_headers["date"] = now

        self.receive_headers.update(headers)
        self.receive_body.update(body)

        req_headers = RequestHeaders(**headers)
        req_body = RequestBody(**body)

        headers = {k: v for k, v in zip(req_headers._fields, req_headers)}
        body = {k: v for k, v in zip(req_body._fields, req_body)}

        data = {
            "headers": headers,
            "body": body
        }

        res = json.dumps(data).encode(self.ENCODING)
        print(res)
        self.sock.send(res)
        time.sleep(0.0001)

    def register(self, name, password):

        make_request(self, self.register.__name__, locals())

    def login(self, account, password):
        make_request(self, "login", locals())

    def logout(self, account):
        make_request(self, "logout", locals())

    def add_friend(self, number):
        make_request(self, "add_friend", locals())

    def del_friend(self, number):
        make_request(self, "del_friend", locals())

    def create_group(self, name):
        make_request(self, "create_group", locals())

    def add_group(self, number):
        make_request(self, "add_group", locals())

    def del_group(self, number):
        make_request(self, "del_group", locals())

    def send_img(self, pic):
        make_request(self, "send_img", locals())

    def show_friends(self):
        make_request(self, "show_friends", locals())

    def show_groups(self):
        make_request(self, "show_groups", locals())

    def search_friend(self, number):
        make_request(self, "search_friend", locals())

    def search_group(self, name):
        make_request(self, "search_group", locals())

    def modify_info(self, info):
        make_request(self, "modify_info", locals())

    def modify_password(self, password):
        make_request(self, "modify_password", locals())

    def upload_img(self, img):
        make_request(self, "upload_img", locals())

    def send_msg(self, msg):
        make_request(self, "send_msg", locals())

    def close(self):
        self.sock.close()
